package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.Room;
import cn.lg.soar.boot.fc.entity.bo.ChangeBo;
import cn.lg.soar.boot.fc.entity.vo.ChangeVo;
import cn.lg.soar.boot.fc.entity.vo.RoomVo;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.ChangeMapper;
import cn.lg.soar.boot.fc.entity.Change;
import cn.lg.soar.boot.fc.service.ChangeService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 变更表
 * @author ZhuanZ（无密码）
 * @since 2025-02-06
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class ChangeServiceImpl extends BaseServiceImpl<ChangeMapper, Change> implements ChangeService {


    @Override
    public HttpResult<Serializable> create(ChangeBo changeBo) {
        ParameterUtil.notNull(changeBo, "数据不能为空");

        Change change = new Change();


        BeanUtils.copyProperties(changeBo, change);

        int insert = this.getBaseMapper().insert(change);

        boolean flag = SqlHelper.retBool(insert);
        AssertUtil.isTrue(flag, "创建失败");

        System.out.println("change create insert int = " + insert);

        return HttpResult.<Serializable>success("创建成功").setData("success");
    }

    @Override
    public PageResult<ChangeVo> queryResultVo(PageQuery pageQuery, QueryParams params) {
        PageResult<ChangeVo> pageResultVo = new PageResult<>();
        SqueryWapper<Change> squeryWapper = new SqueryWapper<>(Change.class);

        PageResult<Change> pageResult = squeryWapper.query(this, pageQuery, params);

        // 对象拷贝
        BeanUtils.copyProperties(pageResult, pageResultVo);

        // vo封装
        List<ChangeVo> list = new ArrayList<>();
        pageResult.getList().forEach(item-> {
            ChangeVo changeVo = new ChangeVo();

            BeanUtils.copyProperties(item, changeVo);
            list.add(changeVo);
        });
        pageResultVo.setList(list);
        return pageResultVo;
    }

    @Override
    public Change getData(Long id) {
        if (Objects.isNull(id)) {
            return new Change();
        }
        return this.getOne(Wrappers.<Change>lambdaQuery().eq(Change::getId, id));
    }

    @Override
    public ChangeVo getVoData(Long id) {

        ChangeVo changeVo = new ChangeVo();
        if (Objects.isNull(id)) {
            return changeVo;
        }
        Change change = this.getData(id);
        BeanUtils.copyProperties(change, changeVo);
        return changeVo;
    }
}
